﻿using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace SSISComponents
{
    class GoogleMetrics
    {
        private DataTable _Metrics;

        public DataTable Metrics
        {
            get
            {
                return _Metrics;
            }
        }

        public GoogleMetrics()
        {
            CreateDataTable();
            LoadXML();
        }

        private void CreateDataTable()
        {
            _Metrics = new DataTable("Metrics");

            DataColumn dcID = new DataColumn("ID", System.Type.GetType("System.String"));
            _Metrics.Columns.Add(dcID);
            DataColumn dcName = new DataColumn("Name", System.Type.GetType("System.String"));
            _Metrics.Columns.Add(dcName);
            DataColumn dcAPIName = new DataColumn("APIName", System.Type.GetType("System.String"));
            _Metrics.Columns.Add(dcAPIName);
        }

        /// <summary>
        /// Load the dimensions XML ressource
        /// </summary>
        public void LoadXML()
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Metrics);
            var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Metrics")
                      select d;
            foreach (var dimensions in Dim)
            {
                _Metrics.Rows.Add(Row(dimensions.Element("ID").Value.ToString(), dimensions.Element("Name").Value.ToString(), dimensions.Element("APIName").Value.ToString()));
            }
        }

        public Boolean Exist(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Metrics);
            bool _APIName = false;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Metrics")
                          select d;
                foreach (var dAPIName in Dim)
                {
                    if (dAPIName.Element("APIName").Value.ToString() == APIName)
                    {
                        _APIName = true;
                    }
                }
            }
            catch { }
            return _APIName;
        }

        public string GetDescription(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Metrics);
            var Met = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Metrics")
                      where d.Element("APIName").Value == APIName
                      select d;
            string _Description = null;
            foreach (var metrics in Met)
            {
                _Description = metrics.Element("Description").Value.ToString();
            }
            return _Description;
        }

        public string GetDataType(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Metrics);
            string _DataType = null;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Metrics")
                          where d.Element("APIName").Value == APIName
                          select d;
                foreach (var dimensions in Dim)
                {
                    _DataType = dimensions.Element("DataType").Value.ToString();
                }
            }
            catch { }
            return _DataType;
        }

        public string GetLength(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Metrics);
            string _Length = null;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Metrics")
                          where d.Element("APIName").Value == APIName
                          select d;

                foreach (var dimensions in Dim)
                {
                    _Length = dimensions.Element("Length").Value.ToString();
                }
            }
            catch { }
            return _Length;
        }

        public string GetPrecision(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Metrics);
            string _Precision = null;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Metrics")
                          where d.Element("APIName").Value == APIName
                          select d;
                foreach (var dimensions in Dim)
                {
                    _Precision = dimensions.Element("Precision").Value.ToString();
                }
            }
            catch { }
            return _Precision;
        }

        public string GetScale(string APIName)
        {
            MemoryStream ms = new MemoryStream(global::SSISComponents.Properties.Resources.Metrics);
            string _Scale = null;
            try
            {
                var Dim = from d in XElement.Load(XmlTextReader.Create(ms)).Elements("Metrics")
                          where d.Element("APIName").Value == APIName
                          select d;

                foreach (var dimensions in Dim)
                {
                    _Scale = dimensions.Element("Scale").Value.ToString();
                }
            }
            catch { }
            return _Scale;
        }

        public string GetID(string APIName)
        {
            MemoryStream msMetrics = new MemoryStream(global::SSISComponents.Properties.Resources.Metrics);
            var Met = from d in XElement.Load(XmlTextReader.Create(msMetrics)).Elements("Metrics")
                      where d.Element("APIName").Value == APIName
                      select d;
            string ID = null;
            foreach (var metrics in Met)
            {
                ID = metrics.Element("ID").Value.ToString();
            }
            msMetrics.Dispose();
            return ID;
        }

        private DataRow Row(String ID, String Name, String APIName)
        {
            DataRow dr = _Metrics.NewRow();
            dr["ID"] = ID;
            dr["Name"] = Name;
            dr["APIName"] = APIName;
            return dr;
        }

        //private void Create()
        //{
        //    _Metrics = new DataTable("Metrics");

        //    DataColumn dcName = new DataColumn("Name", System.Type.GetType("System.String"));
        //    _Metrics.Columns.Add(dcName);
        //    DataColumn dcValue = new DataColumn("Value", System.Type.GetType("System.String"));
        //    _Metrics.Columns.Add(dcValue);
        //    _Metrics.Rows.Add(Row("Exits", "ga:exits"));
        //    _Metrics.Rows.Add(Row("New Visits", "ga:newVisits"));
        //    _Metrics.Rows.Add(Row("Pageviews", "ga:pageviews"));
        //    _Metrics.Rows.Add(Row("Time on Page", "ga:timeOnPage"));
        //    _Metrics.Rows.Add(Row("Time On Site", "ga:timeOnSite"));
        //    _Metrics.Rows.Add(Row("Visitors", "ga:visitors"));
        //    _Metrics.Rows.Add(Row("Visits", "ga:visits"));
        //    _Metrics.Rows.Add(Row("adClicks", "ga:adClicks"));
        //    _Metrics.Rows.Add(Row("adCost", "ga:adCost"));
        //    _Metrics.Rows.Add(Row("CPC", "ga:CPC"));
        //    _Metrics.Rows.Add(Row("CPM", "ga:CPM"));
        //    _Metrics.Rows.Add(Row("CTR", "ga:CTR"));
        //    _Metrics.Rows.Add(Row("Impressions", "ga:impressions"));
        //    _Metrics.Rows.Add(Row("Unique Pageviews", "ga:uniquePageviews"));
        //    _Metrics.Rows.Add(Row("Item Revenue", "ga:itemRevenue"));
        //    _Metrics.Rows.Add(Row("Item Quantity", "ga:itemQuantity"));
        //    _Metrics.Rows.Add(Row("Transactions", "ga:transactions"));
        //    _Metrics.Rows.Add(Row("Transaction Revenue", "ga:transactionRevenue"));
        //    _Metrics.Rows.Add(Row("Transaction Shipping", "ga:transactionShipping"));
        //    _Metrics.Rows.Add(Row("Transaction Tax", "ga:transactionTax"));
        //    _Metrics.Rows.Add(Row("Unique Purchase", "ga:uniquePurchases"));
        //    _Metrics.Rows.Add(Row("Search Depth", "ga:searchDepth"));
        //    _Metrics.Rows.Add(Row("Search Duration", "ga:searchDuration"));
        //    _Metrics.Rows.Add(Row("Search Exits", "ga:searchExits"));
        //    _Metrics.Rows.Add(Row("Search Refinements", "ga:searchRefinements"));
        //    _Metrics.Rows.Add(Row("Search Uniques", "ga:searchUniques"));
        //    _Metrics.Rows.Add(Row("Search Visits", "ga:searchVisits"));
        //    //_Metrics.Rows.Add(Row("", "ga:goal(n)"));
        //    _Metrics.Rows.Add(Row("Goal Completions All", "ga:goalCompletionsAll"));
        //    //_Metrics.Rows.Add(Row("", "ga:goal(n)Starts
        //    _Metrics.Rows.Add(Row("Goal Starts All", "ga:goalStartsAll"));
        //    //_Metrics.Rows.Add(Row("", "ga:goal(n)Value"));
        //    _Metrics.Rows.Add(Row("Goal Value All", "ga:goalValueAll"));
        //    _Metrics.Rows.Add(Row("Total Events", "ga:totalEvents"));
        //    _Metrics.Rows.Add(Row("Unique Events", "ga:uniqueEvents"));
        //    _Metrics.Rows.Add(Row("Event Value", "ga:eventValue"));
        //}

        public Boolean Check()
        {
            return true;
        }
    }
}